package electricity.controller.devicecontroller;


import electricity.WebSocket.DeviceWebSocketHandler;
import electricity.domain.DeviceInfo;
import electricity.dto.DeviceInfoDto;
import electricity.dto.DeviceQueryDTO;
import electricity.result.Result;
import electricity.service.DeviceService;
import electricity.vo.DeviceVO;
import electricity.vo.HealthyVo;
import electricity.vo.HighRiskVo;
import electricity.vo.PageResult;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.tags.Tags;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/devices")
@Tag(name = "设备管理接口")
@Slf4j
public class DeviceController {

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private DeviceWebSocketHandler deviceWebSocketHandler;

    @GetMapping("/list")
    @Operation(summary = "设备分页查询")
    public Result<PageResult>page(DeviceQueryDTO deviceQueryDTO) {
        log.info("设备分页查询:{}", deviceQueryDTO);

        // 查询设备列表
        PageResult pageResult = deviceService.pageQuery(deviceQueryDTO);

        return Result.success(pageResult);

    }

    @PutMapping("/addorupdate")
    @Operation(summary = "设备更新")
    public Result<DeviceVO> update(@RequestBody DeviceInfoDto deviceInfoDto) {
        log.info("更新设备信息:{}", deviceInfoDto);
        if(deviceInfoDto.getId()!=null) {
            DeviceVO deviceVO = deviceService.update(deviceInfoDto);
            return Result.success(deviceVO);
        }
        else{
            log.info("插入设备信息:{}", deviceInfoDto);
            DeviceVO deviceVO = deviceService.insert(deviceInfoDto);
            return Result.success(deviceVO);
        }
    }

    @PutMapping("/status")
    @Operation(summary = "设备状态更新")
    public Result<DeviceVO> updatestatus(@RequestBody DeviceInfoDto deviceInfoDto) {
        log.info("更新设备状态:{}", deviceInfoDto);

            DeviceVO deviceVO = deviceService.updatestatus(deviceInfoDto);
            return Result.success(deviceVO);

    }



    @DeleteMapping("/remove/{id}")
    @Operation(summary = "设备删除")
    public Result delete(@PathVariable Integer id) {
        log.info("删除设备:{}", id);
        deviceService.delete(id);

        return Result.success();
    }

    @DeleteMapping("/batchDelete")
    @Operation(summary = "设备批量删除")
    public Result batchRemove(@RequestBody List<Integer> ids) {
        log.info("批量删除设备:{}", ids);
        deviceService.batchRemove(ids);

        return Result.success();
    }



    // 可选：触发 WebSocket 更新
    @GetMapping("/trigger-update")
    @Operation(summary = "触发 WebSocket 更新")
    public Result triggerUpdate() {
        deviceWebSocketHandler.sendUpdatesToAll();
        return Result.success("更新已触发");
    }
/*
    @GetMapping("/getparams")
    @Operation(summary = "获取设备实时数据")
    public Result getparams(DeviceQueryDTO deviceQueryDTO){
        log.info("获取设备实时数据:{}", deviceQueryDTO);
        // 查询设备列表
        PageResult pageResult = deviceService.paramspageQuery(deviceQueryDTO);

        return Result.success(pageResult);


    }*/

    @GetMapping("/getParamNamesByDeviceId")
    @Operation(summary = "获取设备参数名称")
    public Result getParamNamesByDeviceId(Integer deviceId){
        log.info("获取设备参数名称:{}",deviceId);
        List<String> paramNames = deviceService.getParamNamesByDeviceId(deviceId);
        return Result.success(paramNames);

    }

    @GetMapping("/gethealthStats")
    @Operation(summary = "获取设备健康统计")
    public Result<HealthyVo> gethealthStats(){
       HealthyVo healthyVo = deviceService.gethealthStats();
       return Result.success(healthyVo);

    }

    @GetMapping("/getHighRisk")
    @Operation(summary = "获取高风险设备")
    public Result<List<HighRiskVo>> getHighRisk(@RequestParam(required = false) Integer deviceId){
         log.info("获取高风险设备:{}",deviceId);
        List<HighRiskVo> highRiskDevices = deviceService.getHighRiskDevices(deviceId);
        return Result.success(highRiskDevices);

    }


}


